var diqu;
var yonghu;

$.get("/yonghu_get", {
}, function(res) {
    yonghu = JSON.parse(res);
});

window.onload = function () {

    //图片切换
    var panduan = 1;
    var zancun;
    function change(){
        if (panduan != 3) {
            $("#pic_change"+panduan).animate({opacity:"0"});
            panduan++;
            setTimeout(function(){
                $("#pic_change"+panduan).animate({opacity:"1"});
            },1000)
        }
        else {
            $("#pic_change"+panduan).animate({opacity:"0"});
            panduan=1;
            setTimeout(function(){
                $("#pic_change"+panduan).animate({opacity:"1"});
            },1000)
        }
    }
    setInterval(change,7000);

    //判断用户名重复
    function usercheeck(username) {
        for (var i = 0 ; i < yonghu.length ; i ++ ){
            if( yonghu[i].username == username ){
                return true;
            }
        }
        return false;
    }

    $.get("/diqu_get", {
    }, function(res) {
        diqu = JSON.parse(res);

        var Main = {
            data(){
                var validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入密码'));
                    } else {
                        if (this.form.checkPass !== '') {
                            this.$refs.form.validateField('checkPass');
                        }
                        callback();
                    }
                };
                var validatePass2 = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请再次输入密码'));
                    } else if (value !== this.form.pass) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                };
                var usernamecheeck = (rule, value, callback) => {
                    if (value == '') {
                        callback(new Error('请输入登录账号'));
                    } else if ( usercheeck(value) != false ){
                        callback(new Error('该账号已存在，请重新输入'));
                    }else {
                        callback();
                    }
                }
                return{
                    form: {
                        radio: '1',
                        username: '',
                        pass: '',
                        checkPass: '',
                        name: '',
                        phone: '',
                        value: '',
                        checkList: []
                    },
                    options: diqu,
                    rules2: {
                        pass: [
                            { validator: validatePass, trigger: 'blur' }
                        ],
                        checkPass: [
                            { validator: validatePass2, trigger: 'blur' }
                        ],
                        username: [
                            { validator: usernamecheeck, trigger: 'blur' }
                        ]
                    },
                }
            },
            methods: {
                qiehuan(key){
                    if(key == 1){
                        $("#form2").animate({opacity:"0"});
                        $("#form2").css("z-index","1");
                        setTimeout(function(){
                            $("#form1").animate({opacity:"1"});
                            $("#form1").css("z-index","10");
                        },500)
                    }else {
                        $("#form1").animate({opacity:"0"});
                        $("#form1").css("z-index","1");
                        setTimeout(function(){
                             $("#form2").animate({opacity:"1"});
                             $("#form2").css("z-index","10");
                        },500)
                    }
                },
                open() {
                    this.$alert('如有问题请联系网站技术人员，QQ号：*********', '帮助', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'success',
                                message: `感谢您的支持`
                            });
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                },
                onSubmit(formName) {
                    var zhizheng = this;
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            $.ajax({
                                type: "POST",
                                url: "/user_zhuce",
                                data: this.form,
                                dataType: "json",
                                success: function (message) {
                                    if (message == 1) {
                                        zhizheng.$alert('注册成功，点击确定后为您跳转登录界面','成功', {
                                            confirmButtonText: '确定',
                                            callback: action => {
                                                window.location.href="/html/1.html";
                                            }
                                        });
                                    }
                                    else {

                                    }
                                },
                                error: function (message) {
                                    $("#request-process-patent").html("提交数据失败！");
                                    alert("真的失败了！我们会尽快与您取得联系");
                                }
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                }
            }
        }
        var Ctor = Vue.extend(Main)
        new Ctor().$mount('#app')

    });


}